//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Collections.Specialized;

namespace OpenLS.Chart
{
    public class DataPoints: IEnumerable<DataPoint>, INotifyCollectionChanged
    {
        readonly Dictionary<int, DataPoint> points = new Dictionary<int, DataPoint>();
        public DataPoint this[int index]
        {
            get
            {
                return points[index];
            }
        }
        public void Add (DataPoint dataPoint)
        {
            points.Add(dataPoint.Index, dataPoint);
            NotifyCollectionChanged();
        }
        public void RemoveAt (int index)
        {
            points.Remove(index);
            NotifyCollectionChanged();
        }
        public bool Contains(int index)
        {
            return points.ContainsKey(index);
        }

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        void NotifyCollectionChanged()
        {
            if (CollectionChanged != null)
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        #endregion

        #region IEnumerable<DataPoint> Members

        public IEnumerator<DataPoint> GetEnumerator()
        {
            return points.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public  void Clear()
        {
            points.Clear();
            NotifyCollectionChanged();
        }
    }
}
